//
//  ProjectCalc.m
//  OutesIOS
//
//  Created by norman on 2018/11/25.
//  Copyright © 2018 norman. All rights reserved.
//

#import "ProjectCalc.h"

@interface ProjectCalc()

@end

@implementation ProjectCalc

+(ProjectCalc *) instance{
    static ProjectCalc *ins;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        ins = [[ProjectCalc alloc] init];
    });
    return ins;
}


/**
 计算室内机设备供冷能力

 @param equipId 设备唯一Id
 @param WaterIn 风机盘管供水温度  7
 @param IndoorDB 室内设计干球温度  26
 @param IndoorWB 室内设计湿球温度 20.3
 @return 供冷能力
 */
- (NSMutableDictionary*)calColdCapcity:(NSString*)equipId withWaterIn:(double)WaterIn withIndoorDB:(double)IndoorDB withIndoorWB:(double)IndoorWB {
    NSMutableDictionary *ret = [NSMutableDictionary new];

    NSDictionary *v = [[DBManager manager]queryOneRowData:[NSString stringWithFormat:@"select min(WaterIn) minWaterIn, max(WaterIn) maxWaterIn, min(IndoorDB) minIndoorDB, max(IndoorDB) maxIndoorDB, min(IndoorWB) minIndoorWB, max(IndoorWB) maxIndoorWB from CoolCorrect where EquipId = '%@'", equipId]];
    if (nil == v) {
        return ret;
    }
    if (WaterIn < [[v objectForKey:@"minWaterIn"] doubleValue]) {
        WaterIn = [[v objectForKey:@"minWaterIn"] doubleValue];
    } else if (WaterIn > [[v objectForKey:@"maxWaterIn"] doubleValue]) {
        WaterIn = [[v objectForKey:@"maxWaterIn"] doubleValue];
    }
    if (IndoorDB < [[v objectForKey:@"minIndoorDB"] doubleValue]) {
        IndoorDB = [[v objectForKey:@"minIndoorDB"] doubleValue];
    } else if (IndoorDB > [[v objectForKey:@"maxIndoorDB"] doubleValue]) {
        IndoorDB = [[v objectForKey:@"maxIndoorDB"] doubleValue];
    }
    if (IndoorWB < [[v objectForKey:@"minIndoorWB"] doubleValue]) {
        IndoorWB = [[v objectForKey:@"minIndoorWB"] doubleValue];
    } else if (IndoorWB > [[v objectForKey:@"maxIndoorWB"] doubleValue]) {
        IndoorWB = [[v objectForKey:@"maxIndoorWB"] doubleValue];
    }
    
    
    NSArray *minList = [[DBManager manager]queryArrayData:[NSString stringWithFormat:@"select * from CoolCorrect where EquipId = '%@' and WaterIn <= %f and IndoorDB <= %f and IndoorWB <= %f order by ((WaterIn - %f)*(WaterIn - %f)/(%f*%f) + (IndoorDB - %f)*(IndoorDB - %f)/(%f*%f) + (IndoorWB - %f)*(IndoorWB - %f)/(%f*%f))", equipId, WaterIn, IndoorDB, IndoorWB, WaterIn, WaterIn, WaterIn, WaterIn, IndoorDB, IndoorDB, IndoorDB, IndoorDB, IndoorWB, IndoorWB, IndoorWB, IndoorWB]];
     NSArray *maxList = [[DBManager manager]queryArrayData:[NSString stringWithFormat:@"select * from CoolCorrect where EquipId = '%@' and WaterIn >= %f and IndoorDB >= %f and IndoorWB >= %f order by ((WaterIn - %f)*(WaterIn - %f)/(%f*%f) + (IndoorDB - %f)*(IndoorDB - %f)/(%f*%f) + (IndoorWB - %f)*(IndoorWB - %f)/(%f*%f))", equipId, WaterIn, IndoorDB, IndoorWB, WaterIn, WaterIn, WaterIn, WaterIn, IndoorDB, IndoorDB, IndoorDB, IndoorDB, IndoorWB, IndoorWB, IndoorWB, IndoorWB]];
    if (minList.count > 0 && maxList.count > 0) {
        NSDictionary *min = [minList objectAtIndex:0];
        NSDictionary *max = [maxList objectAtIndex:0];
        
        double dWaterInScale = 0;
        if ([[max objectForKey:@"WaterIn"]doubleValue] > [[min objectForKey:@"WaterIn"]doubleValue]) {
            dWaterInScale = fabs([[min objectForKey:@"WaterIn"]doubleValue] - WaterIn)/fabs([[max objectForKey:@"WaterIn"]doubleValue] - [[min objectForKey:@"WaterIn"]doubleValue]);
        }
        double dIndoorDBScale = 0;
        if ([[max objectForKey:@"IndoorDB"]doubleValue] > [[min objectForKey:@"IndoorDB"]doubleValue]) {
            dIndoorDBScale = fabs([[min objectForKey:@"IndoorDB"]doubleValue] - WaterIn)/fabs([[max objectForKey:@"IndoorDB"]doubleValue] - [[min objectForKey:@"IndoorDB"]doubleValue]);
        }
        double dIndoorWBScale = 0;
        if ([[max objectForKey:@"IndoorWB"]doubleValue] > [[min objectForKey:@"IndoorWB"]doubleValue]) {
            dIndoorWBScale = fabs([[min objectForKey:@"IndoorWB"]doubleValue] - WaterIn)/fabs([[max objectForKey:@"IndoorWB"]doubleValue] - [[min objectForKey:@"IndoorWB"]doubleValue]);
        }
        double d = fabs([[min objectForKey:@"TotalCapacity"]doubleValue] - [[max objectForKey:@"TotalCapacity"]doubleValue]) / ([[min objectForKey:@"TotalCapacity"]doubleValue] - [[max objectForKey:@"TotalCapacity"]doubleValue]);
        if ([[min objectForKey:@"TotalCapacity"]doubleValue] == [[max objectForKey:@"TotalCapacity"]doubleValue]) {
            d = 1;
        }
        double sub = [[max objectForKey:@"TotalCapacity"]doubleValue] - [[min objectForKey:@"TotalCapacity"]doubleValue];
        double tt = ((dWaterInScale > 0 ? 1 : 0) + (dIndoorDBScale > 0 ? 1 : 0) + (dIndoorWBScale > 0 ? 1 : 0));
        double dValue1 = tt > 0 ? [[min objectForKey:@"TotalCapacity"]doubleValue] - (sub * d * dWaterInScale + sub * d * dIndoorDBScale + sub * d * dIndoorWBScale) / tt : [[min objectForKey:@"TotalCapacity"]doubleValue];
        
        d = fabs([[min objectForKey:@"SensibleCapacity"]doubleValue] - [[max objectForKey:@"SensibleCapacity"]doubleValue]) / ([[min objectForKey:@"SensibleCapacity"]doubleValue] - [[max objectForKey:@"SensibleCapacity"]doubleValue]);
        if ([[min objectForKey:@"SensibleCapacity"]doubleValue] == [[max objectForKey:@"SensibleCapacity"]doubleValue]) {
            d = 1;
        }
        sub = [[max objectForKey:@"SensibleCapacity"]doubleValue] - [[min objectForKey:@"SensibleCapacity"]doubleValue];
        tt = ((dWaterInScale > 0 ? 1 : 0) + (dIndoorDBScale > 0 ? 1 : 0) + (dIndoorWBScale > 0 ? 1 : 0));
        double dValue2 = tt > 0 ? [[min objectForKey:@"SensibleCapacity"]doubleValue] - (sub * d * dWaterInScale + sub * d * dIndoorDBScale + sub * d * dIndoorWBScale) / tt : [[min objectForKey:@"SensibleCapacity"]doubleValue];
        [ret setObject:[NSNumber numberWithDouble:dValue1] forKey:@"TotalCapacity"];
        [ret setObject:[NSNumber numberWithDouble:dValue2]  forKey:@"SensibleCapacity"];
    }
    return ret;
}

- (NSArray *)calcAllSnjEquip {
    [[DBManager manager]open:DB_FCU];
    NSArray *allRecord = [[DBManager manager]queryArrayData:@"select * from FCU"];
    for (NSDictionary *equip in allRecord) {
        NSString *Id = [equip objectForKey:@"Id"];
        NSDictionary *data = [self calColdCapcity:Id withWaterIn:7 withIndoorDB:26 withIndoorWB:20.3];
        if (data != nil) {
            for (NSString *key in data.allKeys) {
                [equip setValue:[data objectForKey:key] forKey:key];
            }
        }
    }
    [allRecord sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary * obj2) {
        if ([[obj1 objectForKey:@"TotalCapacity"] doubleValue] < [[obj2 objectForKey:@"TotalCapacity"] doubleValue]) {
            return NSOrderedAscending;
        } else if ([[obj1 objectForKey:@"TotalCapacity"] doubleValue] == [[obj2 objectForKey:@"TotalCapacity"] doubleValue]) {
            return NSOrderedSame;
        } else {
            return NSOrderedDescending;
        }
    }];
    [[DBManager manager]close];
    return allRecord;
}

- (NSArray *)calcAllSwjEquip {
    [[DBManager manager]open:DB_WaterEquip];
    NSArray *allRecord = [[DBManager manager]queryArrayData:@"select * from WaterEquip"];
    for (NSDictionary *equip in allRecord) {
        NSString *Id = [equip objectForKey:@"Id"];
        NSDictionary *data = [self calColdCapcity:Id withWaterIn:7 withIndoorDB:26 withIndoorWB:20.3];
        if (data != nil) {
            for (NSString *key in data.allKeys) {
                [equip setValue:[data objectForKey:key] forKey:key];
            }
        }
    }
    [allRecord sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary * obj2) {
        if ([[obj1 objectForKey:@"TotalCapacity"] doubleValue] < [[obj2 objectForKey:@"TotalCapacity"] doubleValue]) {
            return NSOrderedAscending;
        } else if ([[obj1 objectForKey:@"TotalCapacity"] doubleValue] == [[obj2 objectForKey:@"TotalCapacity"] doubleValue]) {
            return NSOrderedSame;
        } else {
            return NSOrderedDescending;
        }
    }];
    [[DBManager manager]close];
    return allRecord;
}

@end
